package mobile.TheOldRepublic;

import mobile.TheOldRepublic.util.Constants;
import mobile.TheOldRepublic.web.WebViewActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Dashboard extends Activity {
	
	public static final String FORUM_URL = "http://www."+Constants.FORUM_URL;
	private int currentView = -1;
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        
       loadMain();
    }
        
    private void loadMain(){
        setContentView(R.layout.main);
        LinearLayout buttons = (LinearLayout)findViewById(R.id.buttonLayout);
        
        LayoutInflater vi = (LayoutInflater)getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        
        // Dashboard layout made more dynamic for easier maintenance. 20110415LP
        int itemsPerRow = 
        	(Configuration.ORIENTATION_LANDSCAPE == getResources().getConfiguration().orientation)
        	? 3 : 2;
        
        InterfaceButton[] buttonList = new InterfaceButton[]{
        	new InterfaceButton(viewNewsListener, "News", R.drawable.button_news),
        	new InterfaceButton(viewMediaListener, "Media", R.drawable.button_media),
        	new InterfaceButton(viewForumListener, "Forums", R.drawable.button_forum)
        };
        
        LinearLayout row = null;
        for(int cnt = 0; cnt < buttonList.length; cnt++){
        	if(cnt == 0 || (cnt % itemsPerRow == 0)){
        		row = (LinearLayout)vi.inflate(R.layout.blank_layout, null, false);
        		row.setGravity(Gravity.CENTER_HORIZONTAL);
                buttons.addView(row);
        	}
        	row.addView(genButton(buttonList[cnt]));
        }
    }
    
    private ImageView genButton(InterfaceButton btn){
    	ImageView button = new ImageView(this);
		button.setImageResource(btn.image);
		button.setPadding(10, 5, 10, 5);
		button.setOnClickListener(btn.listener);
		
		return button;
    }
    
    private class InterfaceButton{
    	public OnClickListener listener;
    	public String text;
    	public int image;
    	
    	public InterfaceButton(OnClickListener l, String t, int i){
    		listener = l; text = t; image = i;
    	}
    }
    
    private OnClickListener viewNewsListener = new OnClickListener() {
    	public void onClick(View v){
			final Context activity = v.getContext();
			Toast.makeText(activity, "Loading Content, Please Wait...", Toast.LENGTH_SHORT).show();			
            Intent myIntent = new Intent(activity, NewsView.class);
            activity.startActivity(myIntent);
    	}
    };
    
    private OnClickListener viewMediaListener = new OnClickListener() {
    	public void onClick(View v){
			final Context activity = v.getContext();	
			Toast.makeText(activity, "Loading Content, Please Wait...", Toast.LENGTH_LONG).show();		
            Intent myIntent = new Intent(activity, MediaView.class);
            activity.startActivity(myIntent);
    	}
    };
    
    private OnClickListener viewForumListener = new OnClickListener() {
    	public void onClick(View v){
			final Context activity = v.getContext();
	        Intent myIntent = new Intent(activity, WebViewActivity.class);
	        myIntent.putExtra(Constants.KEY_URL, FORUM_URL);
	        activity.startActivity(myIntent);
    	}
    };
    
    @Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		final Context context = this;
		
		menu.clear();
		
		/*MenuItem settings = menu.add("Settings");
		settings.setIcon(android.R.drawable.ic_menu_preferences);
		settings.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem arg0) {
//				Intent preferences = new Intent(context, Preferences.class);
//				context.startActivity(preferences);
				return true;
			}
			
		});*/
		
		MenuItem web = menu.add("TOR Site").setIcon(android.R.drawable.ic_menu_myplaces);
		web.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item){
		        Intent myIntent = new Intent(context, WebViewActivity.class);
		        myIntent.putExtra(Constants.KEY_URL, "http://www.swtor.com/info/systems/advanced-classes");
		        context.startActivity(myIntent);
				
				return true;
			}
		});
		
		MenuItem about = menu.add("About").setIcon(android.R.drawable.ic_menu_info_details);

		about.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				setContentView(R.layout.about);

				return true;
			}

		});
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		// Return to dashboard from sub-views instead of popping out of the app. LP
		if(keyCode == KeyEvent.KEYCODE_BACK && currentView == R.layout.about){
			loadMain();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override 
	public void setContentView(int contentId){
		this.currentView = contentId;
		super.setContentView(contentId);
	}
}